//
//  XQLoginCodeView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQLoginCodeView: UIView {
    
    var textFielHandle: ((String?) -> Void)?
    
    var sendHandle: (() -> Void)?
    
    private lazy var codeTextField : UITextField = {
        let textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        textField.keyboardType = .numberPad
        textField.textColor = .xq_black
        textField.attributedPlaceholder = NSAttributedString(string: "Enter verification code",
                                                              attributes: [
                            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20),
                            NSAttributedString.Key.foregroundColor: UIColor(hex: "#A3A6A9")!])
        textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textField
    }()
    
    public lazy var sendBtn : XQSendCodeButton = {
        let btn = XQSendCodeButton(type: .custom)
        btn.setTitle("Send", for: .normal)
        btn.setTitleColor(.mainTextColor, for: .normal)
        btn.setTitleColor(.mainTextColor.withAlphaComponent(0.5), for: .disabled)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        btn.addTarget(self, action: #selector(sendClick), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    public func becomeResponder() {
        self.codeTextField.becomeFirstResponder()
    }
    
    public func clearNumber() {
        codeTextField.text = ""
        errorTextField()
    }
    
    private func makeUI() {
        self.layer.cornerRadius = 30
        self.backgroundColor = UIColor(hex: "#F6F6F6")
        self.addSubview(self.codeTextField)
        self.addSubview(self.sendBtn)
    }
    
    private func makeConstraints() {
        self.sendBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
        }
        self.codeTextField.snp.makeConstraints { make in
            make.top.bottom.equalTo(0)
            make.left.equalTo(20)
            make.width.equalTo(200)
        }
    }
    
    private func errorTextField() {
        let ani = CABasicAnimation(keyPath: "position")
        ani.duration = 0.05
        ani.repeatCount = 2
        ani.autoreverses = true
        ani.fromValue = NSValue(cgPoint: CGPoint(x: self.center.x - 10, y: self.center.y))
        ani.toValue = NSValue(cgPoint: CGPoint(x: self.center.x + 10, y: self.center.y))
        
        self.layer.add(ani, forKey: "position")
    }
    
    @objc private func textFieldDidChange(_ textField: UITextField) {
        textFielHandle?(textField.text)
    }
    
    @objc private func sendClick() {
        sendHandle?()
    }
    

}
